/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.bjj.ui;

import com.bff.bjj.utils.Resources;
import com.bff.startup.StartUpProperties;
import com.bff.startup.StartupState;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowStateListener;
import javax.swing.Icon;
import javax.swing.Timer;
import javax.swing.JFrame;

/**
 *
 * @author Bill
 */
public abstract class BJJFrame extends JFrame implements ComponentListener, WindowStateListener {

    boolean timerStarted = false;
    private Timer timer;
    private int oldX, oldY;
    private StartUpProperties sup;
    private StartupState startupState;

    public BJJFrame() {
        sup = StartUpProperties.getInstance();

        setStartupState(sup.getStartupState());

        ActionListener al = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                if (isLoaded()) {
                    if (getExtendedState() == JFrame.MAXIMIZED_BOTH) {
                        getSup().writeState(new StartupState(StartupState.State.STATE_MAX, isMiniPlayer()));
                        timer.stop();
                        timerStarted = false;
                    } else {
                        if (getX() == oldX && getY() == oldY) {
                            getSup().writeState(new StartupState(getSize(), getX(), getY(), isMiniPlayer()));
                            timer.stop();
                            timerStarted = false;
                        } else {
                            oldX = getX();
                            oldY = getY();
                        }
                    }
                }
            }
        };
        timer = new javax.swing.Timer(150, al);
        addComponentListener(this);
        addWindowStateListener(this);
    }

    public abstract boolean isMiniPlayer();

    public abstract boolean isLoaded();

    @Override
    public void componentResized(ComponentEvent ce) {
        if (isLoaded()) {
            if (ce.getComponent() instanceof JFrame) {
                JFrame f = (JFrame) ce.getComponent();
                if (f.getExtendedState() == JFrame.MAXIMIZED_BOTH) {
                    getSup().writeState(new StartupState(StartupState.State.STATE_MAX, isMiniPlayer()));
                } else {
                    getSup().writeState(new StartupState(getSize(), getX(), getY(), isMiniPlayer()));
                }
            }
        }
    }

    @Override
    public void componentMoved(ComponentEvent ce) {
        if (timerStarted == false) {
            timerStarted = true;
            timer.start();
        }
    }

    @Override
    public void componentShown(ComponentEvent ce) {
    }

    @Override
    public void componentHidden(ComponentEvent ce) {
    }

    @Override
    public void windowStateChanged(java.awt.event.WindowEvent evt) {
        if (isLoaded() && getState() == JFrame.ICONIFIED) {
            getSup().writeState(new StartupState(StartupState.State.STATE_MIN, isMiniPlayer()));
        }
    }

    /**
     * @return the sup
     */
    public StartUpProperties getSup() {
        return sup;
    }

    /**
     * @param sup the sup to set
     */
    public void setSup(StartUpProperties sup) {
        this.sup = sup;
    }

    public abstract void positionWindow();

    /**
     * @return the startupState
     */
    public StartupState getStartupState() {
        return startupState;
    }

    /**
     * @param startupState the startupState to set
     */
    private void setStartupState(StartupState startupState) {
        this.startupState = startupState;
    }

    @Override
    public void setVisible(boolean b) {
        StartupState ss = getStartupState();
        if (sup.isUseLastState() && ss != null) {
            if (ss.getState() == StartupState.State.STATE_MAX) {
                setExtendedState(JFrame.MAXIMIZED_BOTH);
            } else if (ss.getState() == StartupState.State.STATE_MIN) {
                setExtendedState(JFrame.ICONIFIED);
            } else {
                setSize(ss.getDimension());
                setBounds(ss.getX(),
                        ss.getY(),
                        (int) ss.getDimension().getWidth(),
                        (int) ss.getDimension().getHeight());
            }
        } else {
            positionWindow();
        }
        super.setVisible(b);
    }
    
    protected String getResource(Resources.LABEL_KEY key) {
        return Resources.getMessage(key);
    }
    
    protected Icon getIcon(Resources.ICON_KEY key) {
        return Resources.getIcon(key);
    }
}
